package org.groupg.practice.configuration;

import org.apache.commons.configuration2.XMLConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.ex.ConfigurationException;

import java.util.List;

public class XmlConfigLoader {
    public static void main(String[] args) {
        Parameters params = new Parameters();
        FileBasedConfigurationBuilder<XMLConfiguration> builder =
            new FileBasedConfigurationBuilder<>(XMLConfiguration.class)
                .configure(params.xml()
                    .setFileName("config.xml")
                    .setSchemaValidation(true)); // 启用 Schema 校验

        try {
            XMLConfiguration config = builder.getConfiguration();

            // 读取数组属性
            List<Object> modules = config.getList("app.modules.module");
            List<Object> hosts = config.getList("app.hosts.host");
            String dbUrl = config.getString("app.database.url");

            System.out.println("Modules: " + modules);
            System.out.println("Hosts: " + hosts);
            System.out.println("DB URL: " + dbUrl);
        } catch (ConfigurationException e) {
            System.err.println("XML 配置错误: " + e.getMessage());
        }
    }
}